<template>
  <div class="building-container" v-auth-btn="'park:building:list'">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇姓名：</div>

      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.name"
        @close="doSearch"
      />
      <el-button type="primary" @click="doSearch" v-auth-btn="'park:building:list'">查询</el-button>
    </div>

    <div class="create-container">
      <el-button type="primary" @click="addBuilding" v-auth-btn="'park:building:add_edit'">添加楼宇</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="BuildingList">
        <el-table-column type="index" label="序号" width="150" />
        <el-table-column label="楼宇名称" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" prop="area" />
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" />
        <el-table-column label="状态" width="180">
          <template #default="scope">
            <el-tag :type="formatInfoType(scope.row.status)">
              {{ formatStatus(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editData(scope.row.id)"
              v-auth-btn="'park:building:add_edit'">编辑</el-button>
            <el-button size="mini" type="text" @click="DeleteData(scope.row.id)"
              v-auth-btn="'park:building:remove'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="480px"
        @close="close"
      >
        <div class="form-container">
          <el-form
            :model="formData"
            :rules="rules"
            ref="formData"
            label-width="120px"
          >
            <el-form-item label="楼宇名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>

            <el-form-item label="层数" prop="floors">
              <el-input v-model="formData.floors" />
            </el-form-item>

            <el-form-item label="在管面积(m²)" prop="area">
              <el-input v-model="formData.area" />
            </el-form-item>

            <el-form-item label="物业费(元/m²)" prop="propertyFeePrice">
              <el-input v-model="formData.propertyFeePrice" />
            </el-form-item>

            <el-form-item label="员工状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio :label="0">空闲</el-radio>
                <el-radio :label="1">租赁</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <el-button type="danger" @click="close">取 消</el-button>
          <el-button type="primary" @click="confirmsubmit">确 定</el-button>
        </template>
      </el-dialog>
    </div>

    <div class="page-container">
      <el-pagination
        :current-page="params.page"
        :page-size="params.pageSize"
        :total="total"
        :page-sizes="pageSizes"
        background
        layout="total, sizes,prev, pager, next"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import {
  getBuildingListAPI,
  addBuildingAPI,
  BuildingDetailAPI,
  updateBuildingAPI,
  deleteBuildingAPI,
} from "@/api/Building";

export default {
  name: "Building",
  data() {
    return {
      total: 0,
      title: "",
      dialogVisible: false,
      params: {
        page: 1,
        pageSize: 10,
        name: "",
      },
      pageSizes: [2, 5, 10, 20],
      BuildingList: [],
      formData: {
        id: "",
        name: "",
        propertyFeePrice: 0,
        floors: 0,
        status: 1,
        area: 0,
      },
      rules: {
        name: [{ required: true, message: "请输入楼宇名称", trigger: "blur" }],
        floors: [
          { required: true, message: "请输入租赁层数", trigger: "blur" },
        ],
        area: [{ required: true, message: "请输入在管面积", trigger: "blur" }],
        propertyFeePrice: [
          { required: true, message: "请输入物业费单价", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择楼宇状态", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getBuildingList();
  },
  methods: {
    async getBuildingList() {
      let res = await getBuildingListAPI(this.params);
      this.total = res.data.total;
      this.BuildingList = res.data.rows;
    },
    handleCurrentChange(currentPage) {
      this.params.page = currentPage;
      this.getBuildingList();
    },
    handleSizeChange(size) {
      this.params.pageSize = size;
      this.getBuildingList();
    },
    doSearch() {
      this.getBuildingList();
    },
    addBuilding() {
      this.title = "添加楼宇";
      this.dialogVisible = true;
      this.formData = {};
    },

    async editData(id) {
      this.title = "编辑楼宇";
      this.dialogVisible = true;
      let res = await BuildingDetailAPI(id);
      console.log(res.data);
      this.formData = res.data;
    },
    close() {
      this.dialogVisible = false;
      this.$refs.formData.resetFields();
    },
    confirmsubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (!valid) return;
        if (this.title === "编辑楼宇") {
          const { id, name, floors, area, propertyFeePrice } = this.formData;
          const data = { id, name, floors, area, propertyFeePrice };
          try {
            let res = await updateBuildingAPI(data);
            this.$message.success(res.msg);
          } catch (err) {
            this.$message.error(err.msg);
          }
        } else {
          const { name, floors, area, propertyFeePrice } = this.formData;
          const data = { name, floors, area, propertyFeePrice };
          addBuildingAPI(data)
            .then((res) => {
              this.$message.success(res.msg);
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
        this.getBuildingList();
        this.close();
      });
    },
    formatInfoType(status) {
      const MAP = {
        0: "danger",
        1: "success",
      };
      // return 格式化之后的中文显示
      return MAP[status];
    },
    formatStatus(type) {
      //中括号取值法
      const TYPEMAP = {
        0: "空置中",
        1: "租赁中",
      };
      return TYPEMAP[type];
    },
    DeleteData(id) {
      this.$confirm("确认删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteBuildingAPI(id);
          this.$message.success("删除成功");
          this.getBuildingList();
        })
        .catch((error) => {
          this.$message.info("已取消操作");
        });
    },
  },
  watch: {
    "params.name"(newVal, oldVal) {
      if (!newVal) {
        this.getBuildingList();
      }
    },
    deep: true,
  },
};
</script>

<style lang="scss" scoped>
.building-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
